import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
//注册组件
import Student from '../components/adminModule/Student'
import Charts from "../components/adminModule/Charts"
import Admin from "../components/adminModule/Admin"
import Dorm from "../components/adminModule/Dorm"
import Maintenance from "../components/adminModule/Maintenance"
import Emp from "../components/adminModule/Emp"
import Late from "../components/adminModule/Late"
import Repair from "../components/adminModule/Repair"
import Role from "../components/adminModule/Role"
import Visit from "../components/adminModule/Visit"
import Zhu from "../components/adminModule/Zhu"


//注册页面组件
import LoginView from '../views/LoginView'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


const routes = [
  {
    path:'/logOut',
    name:'logout',
    redirect: '/'
  },
  {
    path:'/',
    name:'login',
    component:LoginView,
    
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: '/Student',
        name: 'Student',
        component: Student,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1' || sessionStorage.getItem("rid")==='2' ){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Chart',
        name:'chart',
        component:Charts,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1'  || sessionStorage.getItem("rid")==='2' ){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Admin',
        name:'admin',
        component:Admin,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1' ){
              next()
            }else{
              alert('权限不够')
              console.log(to);
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Dorm',
        name:'dorm',
        component:Dorm,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1'){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Emp',
        name:'emp',
        component:Emp,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1'){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Maintenance',
        name:'maintenance',
        component:Maintenance,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1'  || sessionStorage.getItem("rid")==='2' ){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Late',
        name:'late',
        component:Late,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1' ){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Repair',
        name:'repair',
        component:Repair,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1'  || sessionStorage.getItem("rid")==='2' ){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Role',
        name:'role',
        component:Role,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1' ){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Visit',
        name:'visit',
        component:Visit,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1'){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      },
      {
        path:'/Zhu',
        name:'zhu',
        component:Zhu,
        meta:{isAuth:true},
        beforeEnter: (to, from, next) => {
          console.log('前置路由守卫',to,from)
          if(to.meta.isAuth){ //判断是否需要鉴权
            if(sessionStorage.getItem("rid")==='1' || sessionStorage.getItem("rid")==='2'){
              next()
            }else{
              alert('权限不够')
            }
          }else{
            next()
          }
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  // base: process.env.BASE_URL,
  routes
})


export default router
